package me.zhengjie.nmi;

import com.alibaba.fastjson.JSONObject;
import me.zhengjie.mi.neww.MyEasyGmUtils;
import me.zhengjie.utils.DateTimeUtil;
import me.zhengjie.utils.HttpClientUtils;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * @Author: 夏洪波
 * @Date: 2024/11/12 16:30
 * @Description:
 */
public class Test8210 {

/*
    private static String pno = "P22011103682";
    private static String LI = "220100";
    private static String CL = "848607E9D9E1CC8E";
    private static String SC = "5936MY";
    private static String USERNAME = "5936";
    private static String PASS = "5936MM";
    private static String SM2PRI = "C2Qf3Dy5AGqV73V92KWcHgeFcq0y+vIAzPQVGDUPwn0=";
    private static String SM2PUB = "BG8Aj4rpx+4lB+2E3/QjftRE0yxKNlHkrJHit6p61oSNDXAVCfL2Qfbdp03czAzXg/p54Rteu3d9jLvdPhzAUfU=";
    private static String serverUrl = "http://ddjk.jlhs.gov.cn:20215/mbs/fmi/fsi/api/callFsiService/callService";
    private static String tokenUrl = "http://ddjk.jlhs.gov.cn:20215/auth/oauth/token";
    */





    private static String pno = "LO220000000010";
    private static String pname = "测试护理机构接口专用一";
    private static String LI = "229900";
    private static String CL = "BD2672C9D52B888B";
    private static String SC = "111111";
    private static String USERNAME = "hlcs220000000010";
    private static String PASS = "111111";
    private static String SM2PRI = "AMspSfr0O7iBjDwkKFRYfG6n0ODSRvEHx96I6h+ypN4l";
    private static String serverUrl = "http://10.47.0.126:8080/mbs/fmi/fsi/api/callHsiService/callService";
    private static String tokenUrl = "http://10.47.0.126:8080/auth/oauth/token";

    /**
     * @param args
     */

    public static void main(String[] args) {

        String token = getToken();
        JSONObject tokenJ = JSONObject.parseObject(token);
        String authorization = tokenJ.getString("access_token");
        System.out.println("[token]:" + authorization);

        String longterm_bchno = "2000000000001";
        String longterm_task_rec_bchno = "2000000000001-01";

        String longterm_exe_detl_codg = "2000000000001-001";
        String longterm_plan_detl_codg = "2000000000001-002";
        String longterm_detl_id = "2000000000001-003";
        String longterm_plan_detl_id = "2000000000001-004";

        List<JSONObject> services = new ArrayList<>();
        for (int i = 0; i < 1; i++) {

            JSONObject servObject = new JSONObject();
            /* 长期护理执行明细编码 */
            servObject.put("longterm_exe_detl_codg", longterm_exe_detl_codg);
            /* 长期护理计划明细编码 */
            servObject.put("longterm_plan_detl_codg", longterm_plan_detl_codg);
            /* 长期护理明细ID */
            servObject.put("longterm_detl_id", longterm_detl_id);
            /* 长期护理批次号 */
            servObject.put("longterm_bchno", longterm_bchno);
            /* 长期护理任务接收批次号 */
            servObject.put("longterm_task_rec_bchno", longterm_task_rec_bchno);
            /* 长期护理计划明细ID */
            servObject.put("longterm_plan_detl_id", longterm_plan_detl_id);
            /* 长期护理服务类型 */
            servObject.put("longterm_serv_type", "01");
            /* 长期护理服务项目编号 */
            servObject.put("longterm_servitem_no", "L22000001900101");
            /* 长期护理服务项目名称 */
            servObject.put("longterm_servitem_name", "床位费（1-2人间");
            /* 医药机构目录名称 */
            servObject.put("medins_list_name", "");
            /* 护理计划开始时间 */
            servObject.put("longterm_plan_begntime", "2024-12-24 10:10:20");
            /* 护理计划结束时间 */
            servObject.put("longterm_plan_endtime", "2024-12-24 10:10:20");
            /* 护理实际开始时间 */
            servObject.put("longterm_act_begntime", "2024-12-24 10:10:20");
            /* 护理实际结束时间 */
            servObject.put("longterm_act_endtime", "2024-12-24 10:42:22");
            /* 参保所属医保区划 */
            servObject.put("insu_admdvs", "229900");
            /* 参保险种 */
            servObject.put("insutype", "410");
            /* 长期护理时长 */
            servObject.put("longterm_dura", "30");
            /* 使用频次 */
            servObject.put("used_frqu", "");
            /* 长期护理明细来源 */
            servObject.put("longterm_detl_souc", "01");
            /* 单价 */
            servObject.put("pric", "43.0000");
            /* 单次数量 */
            servObject.put("sin_cnt", "1");
            /* 计价单位编号 */
            servObject.put("prcunt_no", "05");
            /* 护理人员姓名 */
            servObject.put("nurscare_psn_name", "王秀娥");
            /* 护理人员编号 */
            servObject.put("nurscare_psn_no", "LN220000000052");
            /* 护理人员手机号 */
            servObject.put("nurscare_psn_mob", "15945034584");
            /* 金额 */
            servObject.put("amt", "43.0000");
            /* 人员编码 */
            servObject.put("psn_no", "22000021000100310527");
            /* 证件类型 */
            servObject.put("cert_type", "01");
            /* 证件号码 */
            servObject.put("certno", "220103194008040220");
            /* 人员姓名 */
            servObject.put("psn_name", "侯淑荣");
            /* 护理方式 */
            servObject.put("nurscare_way", "01");
            /* 长期护理评估等级 */
            servObject.put("longterm_eval_lv", "06");
            /* 入住时间 */
            servObject.put("reg_begntime", "2024-12-24 10:20:20");
            /* 长护D登记ID */
            servObject.put("longterm_reg_id", "68001");
            /* 待遇申请ID */
            servObject.put("longterm_trt_appy_id", "44013");
            /* 护理机构编号 */
            servObject.put("nurscare_org_no", "LO220000000010");
            /* 护理机构名称 */
            servObject.put("nurscare_org_name", "测试护理机构接口专用一");
            /* 目录自付比例类别 */
            servObject.put("selfpay_prop_type", "01");
            /*  自付比例 */
            servObject.put("selfpay_prop", "0.0000");
            /* 收费项目等级 */
            servObject.put("chrgitm_lv", "01");
            /* 服务内容 */
            servObject.put("serv_cont", "1-2人间，43元/床、日。以床位和房间房型朝向等分别计算收费的，合并计算在本项目之内。床必须有扶手栏杆和防止滚落、坠落设施，床高度适合老年人使用。人均床位面积、房间床位数量等房间设施要求参照医疗机构病房标准。");
            /* 自费标志 */
            servObject.put("ownpay_flag", "0");
            /* 医疗目录类型 */
            servObject.put("hilist_type", "3");
            /* 定点归属医保区划 */
            servObject.put("fix_blng_admdvs", "229900");
            /* 医疗类别 */
            servObject.put("med_type", "990403");
            /* 费用发生时间 */
            servObject.put("fee_ocur_time", "2024-12-24 10:20:20");
            /* 医疗目录编码 */
            servObject.put("med_list_codg", "");
            /* 医疗目录名称 */
            servObject.put("med_list_name", "");
            /* 医药机构目录编码 */
            servObject.put("medins_list_codg", "");
            /* 明细项目费用总额 */
            servObject.put("det_item_fee_sumamt", "");
            /* 开单科室编码 */
            servObject.put("bilg_dept_codg", "001");
            /* 开单科室名称 */
            servObject.put("bilg_dept_name", "");
            /* 开单医生编码 */
            servObject.put("bilg_dr_codg", "00101");
            /* 开单医师姓名 */
            servObject.put("bilg_dr_name", "");
            /* 受单科室编码 */
            servObject.put("acord_dept_codg", "001");
            /* 受单科室名称 */
            servObject.put("acord_dept_name", "");
            /* 受单医生编码 */
            servObject.put("orders_dr_code", "00101");
            /* 受单医生姓名 */
            servObject.put("orders_dr_name", "");
            /* 医院审批标志 */
            servObject.put("hosp_appr_flag", "0");
            /* 中药使用方式 */
            servObject.put("tcmdrug_used_way", "");
            /* 外检标志 */
            servObject.put("etip_flag", "0");
            /* 外检医院编码 */
            servObject.put("etip_hosp_code", "");
            /* 出院带药标志 */
            servObject.put("dscg_tkdrug_flag", "0");
            /* 备注 */
            servObject.put("memo", "1");
            /* 组套编号 */
            servObject.put("comb_no", "1");
            /* 长期护理执行明细编码 */
            servObject.put("longterm_exe_detl_codg", longterm_exe_detl_codg);
            /* 护理人员标志 */
            servObject.put("nurscare_psn_flag", "01");
            /* 机构定价 */
            servObject.put("medins_pric", "43.0000");
            /* 标准定价 */
            servObject.put("std_pric", "43.0000");
            /* 辅具类别 */
            servObject.put("asst_type", "");
            /* 辅具产品类别 */
            servObject.put("asst_prod_type", "");
            /* 辅具厂商 */
            servObject.put("asst_prdr", "");
            /* 辅具规格 */
            servObject.put("asst_spec", "");
            /* 医保目录编码 */
            servObject.put("hilist_code", "");
            /* 医保目录名称 */
            servObject.put("hilist_name", "");
            services.add(servObject);
        }

        // 22000021000100228164
        // 22000011000014961077
        String input = "{\"batch\": {" +
                "\"longterm_bchno\": \""+longterm_bchno+"\"," +
                "\"longterm_task_rec_bchno\":\""+longterm_task_rec_bchno+"\"," +
                "\"longterm_begn_loc_lnt\":\"125.340647\"," +
                "\"longterm_begn_loc_lat\":\"\"," +
                "\"longterm_begn_loc_addr\":\"\"," +
                "\"longterm_end_loc_lnt\":\"\"," +
                "\"longterm_end_loc_lat\":\"\"," +
                "\"longterm_end_loc_addr\":\"\"," +
                "\"nurscare_psn_crtf_way\":\"01\"," +
                "\"nurscare_psn_crtf_way_memo\":\"\"," +
                "\"insu_crtf_way\":\"01\"," +
                "\"insu_crtf_way_memo\":\"\"," +
                "\"stsf_eval_rslt\":\"10\"," +
                "\"nurscare_eval_flag\":\"1\"," +
                "\"serv_eval_memo\":\"\"," +
                "\"serv_dura_sco\":\"30\"," +
                "\"serv_qlt_sco\":\"90\"," +
                "\"task_detl_souc\":\"\"," +
                "\"longterm_totl_dura\":\"30\"," +
                "\"insutype\":\"410\"," +
                "\"insu_admdvs\":\"22900\"," +
                "\"psn_no\":\"22000021000100310527\"," +
                "\"cert_type\":\"01\"," +
                "\"certno\":\"220103194008040220\"," +
                "\"psn_name\":\"侯淑荣\"," +
                "\"nurscare_way\":\"01\"," +
                "\"longterm_eval_lv\":\"06\"," +
                "\"reg_begntime\":\"20241223\"," +
                "\"longterm_reg_id\":\"68001\"," +
                "\"longterm_trt_appy_id\":\"44013\"," +
                "\"nurscare_org_no\":\"LO220000000010\"," +
                "\"nurscare_org_name\":\"测试护理机构接口专用一\"," +
                "\"longterm_begn_time\":\"2024-12-24 10:10:20\"," +
                "\"longterm_end_time\": \"2024-12-24 10:10:20\"}," +
                "\"services\":" + services + "," +
                "\"att\": []" +
                "}";

        System.out.println(input);
        //签名
        String sign = sign(input);
        //String sign = "";
        //加密
        String sm4Input = encrypt(input);
        //String sm4Input = input;

        System.out.println("sm4Input:"+sm4Input);

        String message = "{" +
                "\"infno\": \"8210\"," +
                "\"msgid\": \"" + pno + ChsUtil.getMsgId() + "\"," +
                "\"mdtrtarea_admvs\": \""+LI+"\"," +
                "\"insuplc_admdvs\": \""+LI+"\"," +
                "\"recer_sys_code\": \"" + ChsInterfaceConfig.recerSysCode001 + "\"," +
                "\"dev_no\": \"\"," +
                "\"dev_safe_info\": \"\"," +
                "\"cainfo\": \"" + sign + "\"," +
                "\"signtype\": \"SM2\"," +
                "\"infver\": \"" + ChsInterfaceConfig.infver + "\"," +
                "\"opter_type\": \"1\"," +
                "\"opter\": \"0001\"," +
                "\"opter_name\": \"test\"," +
                "\"inf_time\": \"" + DateTimeUtil.getDateTime() + "\"," +
                "\"fixmedins_code\": \"" + pno + "\"," +
                "\"fixmedins_name\": \""+pname+"\"," +
                "\"sign_no\": \"1234\"," +
                "\"input\": \"" + sm4Input + "\"}";

        System.out.println("message:"+message);
        JSONObject result = JSONObject.parseObject(handle(message, authorization));
        //System.out.println(message);

        System.out.println(decryption(result.getString("output")));

    }


    /**
     * sm2生成签名
     */
    public static String sign(String inputStr) {
        byte[] privateKey = Base64.getDecoder().decode(SM2PRI);
        byte[] assign = MyEasyGmUtils.signSm3WithSm2(inputStr.getBytes(StandardCharsets.UTF_8),  privateKey);
        return new String(Base64.getEncoder().encode(assign));
    }

    /**
     * sm4加密
     */
    public static String encrypt(String inputStr) {
        byte[] plain = inputStr.getBytes();
        byte[] bs = MyEasyGmUtils.sm4Encrypt(CL.getBytes(StandardCharsets.UTF_8), plain);
        return new String(Base64.getEncoder().encode(bs));
    }

    public static String decryption(String inputStr) {
        byte[] bs = MyEasyGmUtils.sm4Decrypt(CL.getBytes(StandardCharsets.UTF_8), Base64.getDecoder().decode(inputStr));
        return new String(bs);
    }

    /**
     * 获取token
     **/
    public static String getToken() {
        Map<String, String> parmap = new HashMap<String, String>();
        parmap.put("username", USERNAME);
        parmap.put("password", PASS);
        parmap.put("scope", "server");
        parmap.put("grant_type", "password");
        Map<String, String> headermap = new HashMap<String, String>();
        headermap.put("Authorization", "Basic " + Base64.getEncoder().encodeToString((CL + ":" + SC).getBytes(StandardCharsets.UTF_8)));
        headermap.put("Content-Type", "application/x-www-form-urlencoded");
        String result = HttpClientUtils.sendIOPost(tokenUrl, parmap, headermap, Charset.forName("UTF-8"), null);
        System.out.println("[tokenResult]:" + result);
        if (!"".equals(result)) {
            JSONObject json = JSONObject.parseObject(result);
            //有code为错误情况
            if (json.getString("code") == null) {
                System.out.println(json.toJSONString());
            }
        } else {
            result = "{err_msg: '获取token失败,机构信息有误或未进入省平台', code: 500}";
        }
        return result;
    }

    public static String handle(String message, String authorization) {
        Map<String, String> headermap = new HashMap<String, String>();

        headermap.put("Authorization", "Bearer " + authorization);
        headermap.put("Content-Type", "application/json;charset=utf8");
        String result = HttpClientUtils.sendIOPost(serverUrl, null, headermap, Charset.forName("UTF-8"), message);
        System.out.println("[result]" + result);
        return result;
    }


}
